<template>
  <div class="user-manage">
    <div class="top-area filter-container">
      <el-input v-model="params.keywords" placeholder="请输入内容" style="width: 200px;" class="filter-item"
                @keyup.enter.native="searchClick"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchClick">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="addClick" v-if="btn['添加']">
        新增
      </el-button>
      <!-- <el-input v-model="params.keywords" placeholder="请输入内容" style="width: 200px;" class="filter-item" @keyup.enter.native="searchClick" />

      <el-button size="small" type="primary"  class="btnblue" @click="searchClick">搜索</el-button>
      <el-button size="small" type="primary"  class="btnblue" @click="addClick">+ 新增</el-button> -->
    </div>
    <el-table ref="multipleTable" :data="tableData"
              border style="width: 100%" key="personalTab"
              class="bill-table">
      <el-table-column
        prop="name"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="updateClick(scope.row)" v-if="btn['修改']">
            编辑
          </el-button>
          <!-- <el-button type="text" class="setting-default" size="mini" @click="deleteClick(scope.row)">删除</el-button> -->
          <!-- <el-button type="text" size="mini" @click="authorConfigue(scope.row)"></el-button> -->
          <el-button size="mini" type="success" @click="authorConfigue(scope.row)" v-if="btn['权限配置']">
            权限配置
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="params.total" :page.sync="params.pageNo" :limit.sync="params.pageSize"
                @pagination="searchClick"/>
    <el-dialog title="编辑角色" :visible.sync="dialogStatus" @closed="dalogClosed">
      <el-form :model="userObject" class="user" status-icon ref="ruleForm">
        <div class="dialog-form">
          <span class="from-title"><i class="from-title-i">*</i>角色名称</span>
          <el-form-item prop="username" class="el-form-userno">
            <el-input placeholder="30个字以内..." type="text" v-model="userObject.name"></el-input>
          </el-form-item>
        </div>
        <div class="dialog-form">
          <span class="from-title"><i class="from-title-i">*</i>排序</span>
          <el-form-item prop="name">
            <el-input placeholder="10个字以内..." type="text"
                      v-model="userObject.sort"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-form">
        <el-button class="bdnone" size="small" @click="dialogStatus = false">取消</el-button>
        <el-button size="small" type="primary" class="btnblue" @click="commitClick">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="菜单分配"
      :visible.sync="isShowDesignArea"
      @closed="treeClose">
      <div class="el-tree">
        <el-tree
          :data="allMenus"
          show-checkbox
          :props="defaultProps"
          node-key="id"
          accordion
          ref="tree"
          :filter-node-method="filterNode"
          :default-checked-keys="defaultChecks"
          @node-click="handleNodeClick">
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button class="bdnone" size="small" @click="isShowDesignArea = false">取 消</el-button>
        <el-button size="small" class="btnblue" @click="roleSureClick">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {deleteRole, roleList, addUpdateRole, roleGiveMent, getRoleMenu} from "@/api/role.service.js"
  import {menuList} from "@/api/menu.service"

  export default {
    name: 'user',
    components: {Pagination},
    props: {},
    data() {
      return {
        tableData: [],
        dialogTitle: '添加用户',
        // 1 代表添加用户  2代表编辑用户
        type: 1,
        dialogStatus: false,
        userObject: {
          name: '',
          sort: 0,
          id: '',
        },
        params: {
          keywords: "",
          pageNo: 1,
          pageSize: 20,
          total: 0,
        },
        selectRow: {},
        allMenus: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        isShowDesignArea: false,
        defaultChecks: [],
      }
    },
    filters: {},
    watch: {},
    computed: {
      btn() {
        return JSON.parse(localStorage.getItem('btn'))[this.$route.name]
      }
    },
    mounted() {
      this.queryTableData()
      let params = {
        pageNo: 1,
        pageSize: 1000,
        keywords: ''
      }
      this.allMenus = []
      menuList(params).then((res) => {
        res.value.map((item) => {
          item.name = item.menu.name
          item.id = item.menu.id
          if (item.children) {
            item.children.map((val) => {
              val.name = val.menu.name
              val.id = val.menu.id
              if (val.children) {
                val.children.map((value) => {
                  if (val.children) {
                    val.children.map((value1) => {
                      value1.name = value1.menu.name
                      value1.id = value1.menu.id
                    })
                  }
                })
              }
            })
          }
        })
        this.allMenus = res.value
        console.warn(this.allMenus)
      }).catch(error => {

      })
    },
    methods: {
      filterNode(value, data) {
        if (!value) return true;
        return data.nameStr.indexOf(value) !== -1;
      },
      treeClose() {
        this.isShowDesignArea = false
      },
      handleNodeClick() {

      },
      searchClick() {
        this.queryTableData()
      },
      handleCurrentChange(val) {
        this.queryTableData(val)
      },
      handleSizeChange(val) {
        this.params.pageSize = val
        this.queryTableData()
      },
      queryTableData() {
        this.tableData = []
        roleList(this.params).then((res) => {
          if (res.code == 200) {
            this.tableData = res.values
            this.params.total = res.total
          }
        }).catch(error => {

        })
      },
      addClick(row) {
        this.type = 1
        this.selectRow = row
        this.userObject = {
          name: '',
          sort: 0,
          id: ''
        }
        this.dialogStatus = true
      },
      //关闭弹框调用的方法
      dalogClosed() {
        this.dialogStatus = false
      },
      updateClick(row) {
        this.type = 2
        this.selectRow = row
        this.userObject = row
        this.dialogStatus = true
      },
      deleteClick(row) {
        this.selectRow = row
        this.$alert('确认删除改用户么？', '确定删除', {
          confirmButtonText: '确定',
          callback: action => {
            deleteRole(this.selectRow.id).then((res) => {
              this.$message({type: 'success', message: '操作成功'})
              this.queryTableData()
            }).catch((err) => {

            })
          }
        });
      },
      commitClick() {
        this.dialogStatus = false
        //提交
        if (this.type == 1) {
          delete this.userObject.id
        }
        addUpdateRole(this.userObject).then((res) => {
          this.$message({type: 'success', message: '操作成功'})
          this.queryTableData()
        }).catch((err) => {

        })
      },
      authorConfigue(row) {
        this.isShowDesignArea = true
        let tempMenus = JSON.parse(JSON.stringify(this.allMenus))
        this.allMenus = []
        this.allMenus = tempMenus
        this.selectRow = row
        this.defaultChecks = []
        getRoleMenu({roleId: row.id}).then((res) => {
          res.value.map((item) => {
            this.defaultChecks.push(item)
          })
          this.$refs.tree.setCheckedKeys(this.defaultChecks)
        })

      },
      roleSureClick() {
        this.isShowDesignArea = false
        let menuList = this.$refs.tree.getCheckedNodes()
        if (roleList.length == 0) {
          this.$message({type: 'warning', message: '请选择需要分配的角色'})
          return
        }
        let tempIds = []
        menuList.map((item) => {
          tempIds.push(item.id)
        })
        let params = {
          menuId: tempIds.join(','),
          roleId: this.selectRow.id,
        }
        roleGiveMent(params).then((res) => {
          this.$message({type: 'success', message: '分配成功'})
          this.queryTableData()
        }).catch(error => {

        })
      }
    }
  }
</script>

<style scoped>

</style>
<style>
  .user .dialog-form {
    display: flex;
    align-items: center;
    margin-top: 10px;
  }

  .user .dialog-form .from-title {
    width: 70px;
    margin-right: 10px;
  }

  .user .dialog-form .from-title .from-title-i {
    color: red;
  }

  .user .dialog-form .el-form-item {
    flex: 1;
    margin-bottom: 0px;
  }

  .top-area {
    margin-bottom: 20px;
  }

</style>

